package com.aduerr.v1shouyin.print;

import com.aduerr.v1shouyin.unioncard.IView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import java.lang.ref.WeakReference;

/**
 * Created by waidell on 18/9/8.
 */

public class PrintObserver implements Observer<Integer> {


    private WeakReference<IView> viewWeakReference;

    public PrintObserver(IView view) {
        viewWeakReference = new WeakReference<>(view);
    }

    @Override
    public void onSubscribe(Disposable d) {
        IView view = viewWeakReference.get();
        if(view != null) {
            view.addDisposable(d);
            view.showLoadingDialog("正在打印...");
        }
    }

    @Override
    public void onNext(Integer integer) {
        IView view = viewWeakReference.get();
        if(view != null) {
            if(integer > 0) {
                view.showLoadingDialog(String.format("打印纸未准备好，请重新放入打印纸(%d)", integer));
            } else {
                view.showLoadingDialog("正在打印...");
            }
        }

    }

    @Override
    public void onError(Throwable e) {
        IView view = viewWeakReference.get();
        if(view != null) {
            view.dismissLoadingDialog();
            view.showToast("打印失败，原因：" + e.getMessage());
        }
    }

    @Override
    public void onComplete() {
        IView view = viewWeakReference.get();
        if(view != null) {
            view.dismissLoadingDialog();
        }
    }
}

